﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.MODEL.FoundationModel;
using SmallERP.DAL;
using DevExpress.XtraGrid;
using System.Data;


namespace SmallERP.Bll.BaseInfo
{
    public class Printer:IDBAccess
    {
        #region 私有成员


        //private Printer_info printerInfo =null;
        

        #endregion

        #region 属性

        //public Printer_info PrinterInfo
        //{
        //    get { return printerInfo; }
        //    set { printerInfo = value; }
        //}
        #endregion


        #region IDBAccess 成员
        public List<Printer_info> GetPrinterList()
        {
            List<Printer_info> list = new List<Printer_info>();
            //using (SmallERPDBEntity entities = new SmallERPDBEntity())
            //{
            //    List<printer> listPrinter = entities.printer.ToList();
            //    foreach (var printerobj in listPrinter)
            //    {
            //        Printer_info printerInfo = new Printer_info();
            //        printerInfo.HotDirPath = printerobj.hotDirPath;
            //        printerInfo.IsTransverse = printerobj.IsTransverse;
            //        printerInfo.MonitorDirPath = printerobj.MonitorDirPath;
            //        printerInfo.PDes = printerobj.PDes;
            //        printerInfo.PDPI = printerobj.PDPI;
            //        printerInfo.PLength = printerobj.PLength;
            //        printerInfo.PName = printerobj.PName;
            //        printerInfo.PrinterID = printerobj.PrinterID;
            //        printerInfo.PWidth = printerobj.PWidth;
            //        printerInfo.PrintStatus = printerobj.PrintStatus;
            //        printerInfo.PBstatus = printerobj.PBstatus;
            //        list.Add(printerInfo);
            //    }


            //}

            string QueryPrinter = "select * from printer ";
            DataTable Qtable = DAL.AccessHelper.dataTable(QueryPrinter);
            for (int i = 0; i < Qtable.Rows.Count; i++)
            {
                Printer_info printerInfo = new Printer_info();
                printerInfo.HotDirPath = Qtable.Rows[i]["hotDirPath"].ToString();
                printerInfo.IsTransverse = Convert.ToBoolean(Qtable.Rows[i]["IsTransverse"]);
                printerInfo.MonitorDirPath = Qtable.Rows[i]["MonitorDirPath"].ToString();
                printerInfo.PDes = Qtable.Rows[i]["PDes"].ToString(); ;
                printerInfo.PDPI = Convert.ToInt32(Qtable.Rows[i]["PDPI"]); ;
                printerInfo.PLength = Convert.ToSingle(Qtable.Rows[i]["PLength"]);
                printerInfo.PName = Qtable.Rows[i]["PName"].ToString();
                printerInfo.PrinterID = Convert.ToInt32(Qtable.Rows[i]["PrinterID"]);
                printerInfo.PWidth = Convert.ToInt32(Qtable.Rows[i]["PWidth"]);
                printerInfo.PrintStatus = Convert.ToInt32(Qtable.Rows[i]["PrintStatus"]);
                printerInfo.PBstatus = Convert.ToInt32(Qtable.Rows[i]["PBstatus"]);
                printerInfo.IsMiddle = Convert.ToBoolean(Qtable.Rows[i]["IsMiddle"]);
                list.Add(printerInfo);
            }

            return list;
        }
        public int Add(object o)
        {
            int ret = 0;
            Printer_info info =(Printer_info)o;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        printer printer = new printer();
            //        printer.hotDirPath = printerInfo.HotDirPath;
            //        printer.IsTransverse = printerInfo.IsTransverse;
            //        printer.MonitorDirPath = printerInfo.MonitorDirPath;
            //        printer.PDes = printerInfo.PDes;
            //        printer.PDPI = printerInfo.PDPI;
            //        printer.PLength = printerInfo.PLength;
            //        printer.PName = printerInfo.PName;
            //        printer.PWidth = printerInfo.PWidth;
            //        printer.PrintStatus = printerInfo.PrintStatus;
            //        printer.PBstatus = printerInfo.PBstatus;
            //        entities.AddToprinter(printer);
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}
            DataTable dt = DAL.AccessHelper.dataTable("select * from printer where PName='" + info.PName+"'");
            if (dt.Rows.Count == 0)
            {
               
                try
                {
                    string insertSql = "insert into printer (hotDirPath,IsTransverse,MonitorDirPath,PDes,PDPI,PLength,PName,PWidth,PrintStatus,PBstatus,IsMiddle)"
                                       + " values('" + info.HotDirPath + "'," + Convert.ToInt32(info.IsTransverse) + ",'" + info.MonitorDirPath + dota + info.PDes + dota + info.PDPI + dota + info.PLength + dota
                                       + info.PName + dota + info.PWidth + dota + info.PrintStatus + dota + info.PBstatus + "'," + Convert.ToInt32(info.IsMiddle) + ")";
                    ret = DAL.AccessHelper.ExecuteNonQuery(insertSql);
                }
                catch
                {
                    ret = -1;
                }
            }
            else
            {
                return -2;
            }
            return ret;
        }
        string dota = "' , '";
        public int Update(object o)
        {
            int ret = 0;
            Printer_info info = (Printer_info)o;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        printer printer = entities.printer.FirstOrDefault(p => p.PrinterID == printerInfo.PrinterID);
            //        printer.hotDirPath = printerInfo.HotDirPath;
            //        printer.IsTransverse = printerInfo.IsTransverse;
            //        printer.MonitorDirPath = printerInfo.MonitorDirPath;
            //        printer.PDes = printerInfo.PDes;
            //        printer.PDPI = printerInfo.PDPI;
            //        printer.PLength = printerInfo.PLength;
            //        printer.PName = printerInfo.PName;
            //        printer.PWidth = printerInfo.PWidth;
            //        printer.PrintStatus = printerInfo.PrintStatus;
            //        printer.PBstatus = printerInfo.PBstatus;
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}
            string updateDota = " ',";
            DataTable dt = DAL.AccessHelper.dataTable("select * from printer where PrinterID=" + info.PrinterID + "");
             if (dt.Rows.Count != 0)
             {
                 string Sql = "update printer set hotDirPath='" + info.HotDirPath + updateDota + " IsTransverse=" + Convert.ToInt32(info.IsTransverse) + ","  + " MonitorDirPath='" + info.MonitorDirPath +updateDota+ " PDes='" + info.PDes
                               + updateDota + " PLength='" + info.PLength + updateDota + " PName='" + info.PName + updateDota + " PWidth='" + info.PWidth + updateDota + " PrintStatus='" + info.PrintStatus + updateDota + " PBstatus=" + info.PBstatus + ",IsMiddle="+Convert.ToInt32(info.IsMiddle)
                               + " where  PrinterID="+info.PrinterID+"";
                 ret = DAL.AccessHelper.ExecuteNonQuery(Sql);
             }
             else
             {
                 return -2;
             }
            return ret;
        }

        public int Delete(object o)
        {
            int ret = 0;
            Printer_info info = (Printer_info)o;

            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        printer printer = entities.printer.FirstOrDefault(p => p.PrinterID == printerInfo.PrinterID);
            //        entities.DeleteObject(printer);
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}

            DataTable dt = DAL.AccessHelper.dataTable("select * from printer where PrinterID=" + info.PrinterID);
            if (dt.Rows.Count != 0)
            {
                string Sql = "delete from printer where PrinterID=" + info.PrinterID + "";
                ret = DAL.AccessHelper.ExecuteNonQuery(Sql);
            }
            else
            {
                return -2;
            }


            return ret;
        }

        public int LinkGridControl(object o)
        {
            int ret = 0;
            try
            {
                ((GridControl)o).DataSource = GetPrinterList();
                ((GridControl)o).RefreshDataSource();
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }

        #endregion
    }
}
